home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / getopt.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  5.2 KB  |  139 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. """Module getopt -- Parser for command line options.
  5.  
  6. This module helps scripts to parse the command line arguments in
  7. sys.argv.  It supports the same conventions as the Unix getopt()
  8. function (including the special meanings of arguments of the form `-'
  9. and `--').  Long options similar to those supported by GNU software
  10. may be used as well via an optional third argument.  This module
  11. provides a single function and an exception:
  12.  
  13. getopt() -- Parse command line options
  14. error    -- Exception (string) raised when bad options are found
  15. """
  16. import string
  17. error = 'getopt.error'
  18.  
  19. def getopt(args, shortopts, longopts = []):
  20.     '''getopt(args, options[, long_options]) -> opts, args
  21.  
  22.     Parses command line options and parameter list.  args is the
  23.     argument list to be parsed, without the leading reference to the
  24.     running program.  Typically, this means "sys.argv[1:]".  shortopts
  25.     is the string of option letters that the script wants to
  26.     recognize, with options that require an argument followed by a
  27.     colon (i.e., the same format that Unix getopt() uses).  If
  28.     specified, longopts is a list of strings with the names of the
  29.     long options which should be supported.  The leading \'--\'
  30.     characters should not be included in the option name.  Options
  31.     which require an argument should be followed by an equal sign
  32.     (\'=\').
  33.  
  34.     The return value consists of two elements: the first is a list of
  35.     (option, value) pairs; the second is the list of program arguments
  36.     left after the option list was stripped (this is a trailing slice
  37.     of the first argument).  Each option-and-value pair returned has
  38.     the option as its first element, prefixed with a hyphen (e.g.,
  39.     \'-x\'), and the option argument as its second element, or an empty
  40.     string if the option has no argument.  The options occur in the
  41.     list in the same order in which they were found, thus allowing
  42.     multiple occurrences.  Long and short options may be mixed.
  43.  
  44.     '''
  45.     opts = []
  46.     if type(longopts) == type(''):
  47.         longopts = [
  48.             longopts]
  49.     else:
  50.         longopts = list(longopts)
  51.     longopts.sort()
  52.     while args and args[0][:1] == '-' and args[0] != '-':
  53.         if args[0] == '--':
  54.             args = args[1:]
  55.             break
  56.         
  57.         if args[0][:2] == '--':
  58.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  59.         else:
  60.             (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  61.     return (opts, args)
  62.  
  63.  
  64. def do_longs(opts, opt, longopts, args):
  65.     
  66.     try:
  67.         i = string.index(opt, '=')
  68.         (opt, optarg) = (opt[:i], opt[i + 1:])
  69.     except ValueError:
  70.         optarg = None
  71.  
  72.     (has_arg, opt) = long_has_args(opt, longopts)
  73.     if has_arg:
  74.         if optarg is None:
  75.             if not args:
  76.                 raise error, 'option --%s requires argument' % opt
  77.             
  78.             (optarg, args) = (args[0], args[1:])
  79.         
  80.     elif optarg:
  81.         raise error, 'option --%s must not have an argument' % opt
  82.     
  83.     if not optarg:
  84.         pass
  85.     opts.append(('--' + opt, ''))
  86.     return (opts, args)
  87.  
  88.  
  89. def long_has_args(opt, longopts):
  90.     optlen = len(opt)
  91.     for i in range(len(longopts)):
  92.         (x, y) = (longopts[i][:optlen], longopts[i][optlen:])
  93.         if y != '' and y != '=' and i + 1 < len(longopts):
  94.             if opt == longopts[i + 1][:optlen]:
  95.                 raise error, 'option --%s not a unique prefix' % opt
  96.             
  97.         
  98.         if longopts[i][-1:] in ('=',):
  99.             return (1, longopts[i][:-1])
  100.         
  101.         return (0, longopts[i])
  102.     
  103.     raise error, 'option --' + opt + ' not recognized'
  104.  
  105.  
  106. def do_shorts(opts, optstring, shortopts, args):
  107.     while optstring != '':
  108.         (opt, optstring) = (optstring[0], optstring[1:])
  109.         if short_has_arg(opt, shortopts):
  110.             if optstring == '':
  111.                 if not args:
  112.                     raise error, 'option -%s requires argument' % opt
  113.                 
  114.                 (optstring, args) = (args[0], args[1:])
  115.             
  116.             (optarg, optstring) = (optstring, '')
  117.         else:
  118.             optarg = ''
  119.         opts.append(('-' + opt, optarg))
  120.     return (opts, args)
  121.  
  122.  
  123. def short_has_arg(opt, shortopts):
  124.     for i in range(len(shortopts)):
  125.         if shortopts[i] == shortopts[i]:
  126.             pass
  127.         elif shortopts[i] != ':':
  128.             return shortopts[i + 1:i + 2] == ':'
  129.         
  130.     
  131.     raise error, 'option -%s not recognized' % opt
  132.  
  133. if __name__ == '__main__':
  134.     import sys
  135.     print getopt(sys.argv[1:], 'a:b', [
  136.         'alpha=',
  137.         'beta'])
  138.  
  139.